<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="saveDialog">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addLibrary } from '@/api/part/Warehouse';
import { getBrands } from '@/api/part/Warehouse';
// import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { getStorageList } from '@/api/part/common';
export default {
  name: 'librayModel',
  mixins: [dictionary],
  props: {
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    this.flag = false;
    return {
      formList: this.createFormList()
    };
  },
  computed: {},
  mounted() {
    this.init();
    this.getBrands();
  },
  methods: {
    init() {},
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'storageCode',
          placeholder: '请选择',
          itemList: [],
          rules: { required: true, message: '请选择仓库', trigger: 'change' }
        },
        {
          type: 'INPUT',
          label: '库位代码',
          fieldName: 'storagePositionCode',
          placeholder: '请输入',
          rules: [
            { required: true, validator: this.checkStorageCode, message: '库位代码不能为空！', trigger: 'blur' },
            { min: 4, max: 30, message: '长度在 4 到 30 个字符', trigger: 'blur' }
            // { pattern: /[0-9 | a-z | A-z]+/g, message: '库位代码只能为数字，字母的组合', trigger: 'blur' }
          ]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    // 提交
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (!this.saveData) return;
      let res = {};
      res = await addLibrary(this.saveData);
      if (res.resultCode == '200') {
        this.test();
      }
    },
    async getBrands() {
      const res = await getStorageList({});
      // 增加非空判断
      if (res.data.length) {
        this.formList.find(item => item.fieldName === 'storageCode').itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
      this.closeDialog('refresh');
    }
  }
};
</script>

<style lang="less" scoped></style>
